<template>
	<view class="content">
		<view class="menuBox">
			<view class="menuItem" @click="changeMenu(0)">
				<text class="label">BMI免疫评测</text>
				<view class="line2" v-if="menuIndex==0">
				</view>
			</view>
			<view class="line1">
			</view>
			<view class="menuItem" @click="changeMenu(1)">
				<text class="label">定制营养报告</text>
				<view class="line2" v-if="menuIndex==1">
				</view>
			</view>
		</view>
		<scroll-view class="listBox" scroll-y="true" @scrolltolower="getMore()" v-if="menuIndex==0">
			<view class="listItem" v-for="(item,index) in data" :key="index"
				@click="$navto.navto('/pages/questionnaire/questionnaire',{id:item.id})">
				<text class="name">{{item.nickname}}</text>
				<text class="time">{{item.create_time}}</text>
				<text class="text1">体脂率(BMI) :{{item.exponent}} / {{item.bmi_grade_name}}</text>
				<text class="text2">免疫指标 : {{item.immune}} / {{item.immune_grade_name}}</text>
				<view class="line">
				</view>
				<view class="btn">查看报告</view>
			</view>
		</scroll-view>
		<view class="again" v-if="menuIndex==1" @click="showInfo()">查看详情
		</view>
		<view class="again" v-if="menuIndex==1" @click="toBuy()">去购买
		</view>
		<!-- <view class="again" v-if="data.length>0" @click="test()">再次测评
		</view>
		<view class="again" v-if="data.length==0&&menuIndex==1" @click="$navto.navto('/pages/questionnaire/questionnaire',{id:0})">去评测
		</view> -->
		<view class="again" v-if="menuIndex==1" @click="test()">去测评
		</view>
		<view class="again" v-if="menuIndex==0" @click="$navto.navto('/pages/questionnaire/questionnaire',{id:0})">去评测
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page_no: 1,
				data: '',
				menuIndex: 0
			}
		},
		onShow() {
			this.$request.get(`shopapi/Questionnaire/user_list_questionnaire?page_no=${this.page_no}&page_size=10`).then(
				res => {
					console.log(res.data);
					this.data = res.data.data.lists;
				})
		},
		methods: {
			test() {
				if (this.menuIndex == 0) {
					this.$navto.navto('/pages/questionnaire/questionnaire', {
						id: 0
					});
				} else {
					wx.openEmbeddedMiniProgram({
						'appId': 'wxccc1e457b03da613',
						'path': `pages/invitationLand/invitationLand?salesmanId=544852&o_id=${this.userInfo.sn}&phone=${this.userInfo.mobile}`,
					})
				}
			},
			toBuy() {
				this.$request.get(`shopapi/medawell/medawell?sn=${this.userInfo.sn}`).then(res => {
					console.log(res);
					if (res.code == 0) { //未测试
						this.$toast({
							title: res.msg
						})
						return;
					} else {
						wx.openEmbeddedMiniProgram({
							'appId': 'wxccc1e457b03da613',
							'path': `pages/cart/make-order?salesmanId=544852&count_id=${res.data.data.count_id}&is_checkup=1&o_id=${this.userInfo.sn}`,
						})
					}
				})
			},
			showInfo() {
				this.$request.get(`shopapi/medawell/medawell?sn=${this.userInfo.sn}`)
					.then(res => {
						console.log(res);
						if (res.data.code == 0) { //未测试
							this.$toast({
								title: res.data.msg
							})
							return;
						} else {
							// 下载PDF文件
							uni.downloadFile({
								url: res.data.data.pdf_url,
								success(res) {
									const filePath = res.tempFilePath;
									console.log(filePath)
									// 打开PDF文件
									uni.openDocument({
										filePath: filePath,
										fileType: 'pdf',
										success() {
											console.log('打开文档成功');
										},
										fail(err) {
											console.log('打开文档失败', err);
										}
									});
								},
								fail(err) {
									console.log('下载文件失败', err);
								}
							});

						}
					})
			},
			changeMenu(num) {
				this.menuIndex = num;
			},
			getMore() {
				this.page_no++;
				this.$request.get(`shopapi/Questionnaire/user_list_questionnaire?page_no=${this.page_no}&page_size=10`)
					.then(res => {
						this.data.push(...res.data.data.lists);
					})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.menuBox {
		width: 100%;
		display: flex;
		justify-content: space-around;
		align-items: center;
		margin-top: 30rpx;

		.menuItem {
			position: relative;
			flex-direction: column;
			display: flex;
			align-items: center;
			height: 60rpx;

			.line2 {
				position: absolute;
				bottom: 0;
				width: 55rpx;
				height: 4rpx;
				background-color: #f08229;
				margin: 0 auto;
			}
		}

		.line1 {
			height: 35rpx;
			width: 2rpx;
			background-color: #cccccc;
		}
	}

	.again {
		width: 400rpx;
		height: 90rpx;
		margin-top: 80rpx;
		margin-left: 190rpx;
		background-color: #f08229;
		font-size: 29rpx;
		text-align: center;
		line-height: 90rpx;
		border-radius: 40rpx;
		color: #fff;
	}

	.btn {
		width: 144rpx;
		height: 52rpx;
		position: absolute;
		top: 163rpx;
		right: 40rpx;
		background-color: #f08229;
		font-size: 21rpx;
		text-align: center;
		line-height: 52rpx;
		border-radius: 26rpx;
		color: #fff;
	}

	.line {
		width: 550rpx;
		height: 1rpx;
		position: absolute;
		top: 117rpx;
		background-color: #141414;
		left: 40rpx;
	}

	.text1 {
		position: absolute;
		font-size: 20rpx;
		left: 40rpx;
		top: 143rpx;
		font-family: 'ar';
		display: block;
	}

	.text2 {
		position: absolute;
		font-size: 20rpx;
		left: 40rpx;
		top: 190rpx;
		font-family: 'ar';
		display: block;
	}

	.time {
		position: absolute;
		font-size: 20rpx;
		right: 40rpx;
		top: 65rpx;
		font-family: 'ar';
		color: #8e8e8e;
		display: block;
	}

	.name {
		position: absolute;
		font-size: 39rpx;
		left: 40rpx;
		top: 44rpx;
		font-family: 'am';
		display: block;
	}

	.listItem {
		position: relative;
		width: 630rpx;
		height: 250rpx;
		border-radius: 15rpx;
		box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
		margin-bottom: 40rpx;
		margin-top: 10rpx;
		margin-left: 20rpx;
	}

	.listBox {
		width: 670rpx;
		margin-left: 40rpx;
		margin-top: 60rpx;
		max-height: 75vh;
	}

	.label {
		font-size: 39rpx;
		// margin-left: 40rpx;
		// margin-top: 32rpx;
		font-family: 'am';
		display: block;
	}

	.content {
		position: fixed;
		width: 100%;
		height: 100vh;
		overflow-y: scroll;
		overflow-x: hidden;
		background-color: #fff !important;
	}

	page {
		width: 100%;
		height: 100%;
		overflow-y: scroll;
		background-color: #fff !important;
	}
</style>